package cst.a816wzj.zyfypt_816wzj;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2017/5/8.
 */

public class ResModel<T> implements AmicoolIface {
    private Retrofit retrofit;
    private ResService service;
    private static final String BASE_URL="http://amicool.neusoft.edu.cn/";
    private static final String SESSION_ID= "2ts2j910hv4m270hipde4a67d7";
    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else{
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);
    }

    @Override
    public void login(String u, String p, final AmicoolListener l) {
       Call<LoginBean> call = service.login(u,p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String u, String p, String t, int roleid, String email, AmicoolListener l) {

    }

    @Override
    public void getList(String mod, int page, final AmicoolListener l) {
        switch (mod){
            case "video":
                Call<List<VideoBean>> call = service.getvideoList(mod,page,SESSION_ID);
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case"article":
                Call<List<ArticleBean>> call_a=service.getArticleList(mod,page,SESSION_ID);
                call_a.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                Call<List<TwareBean>> call_b=service.getTwareList(mod,page,SESSION_ID);
                call_b.enqueue(new Callback<List<TwareBean>>() {
                    @Override
                    public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
            default:
                break;
        }
    }

    @Override
    public void getVideoDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
